package com.shuoyue.ycgk.ui.shop;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.viewpager.widget.ViewPager;

import com.google.android.material.tabs.TabLayout;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.shuoyue.ycgk.R;
import com.shuoyue.ycgk.appcatch.SPUtils;
import com.shuoyue.ycgk.base.BaseMvpActivity;
import com.shuoyue.ycgk.base.BaseMvpFragment;
import com.shuoyue.ycgk.base.ListWithPage;
import com.shuoyue.ycgk.base.baseadapter.IndexFragmentAdapter;
import com.shuoyue.ycgk.base.baseadapter.IndexFragmentStateAdapter;
import com.shuoyue.ycgk.entity.MyBanner;
import com.shuoyue.ycgk.entity.ShopItem;
import com.shuoyue.ycgk.entity.Type;
import com.shuoyue.ycgk.ui.course.FragmentCourseTab;
import com.shuoyue.ycgk.ui.search.SearchActivity;
import com.shuoyue.ycgk.ui.shop.contract.ShopGoodsContract;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ShopTabActivity extends BaseMvpActivity<ShopGoodsContract.Presenter>
        implements ShopGoodsContract.View {

    @BindView(R.id.page_title)
    TextView pageTitle;
    @BindView(R.id.tab_layout)
    TabLayout tabLayout;
    @BindView(R.id.view_pager)
    ViewPager viewPager;
    @BindView(R.id.refreshlayout)
    SmartRefreshLayout refreshlayout;
    @BindView(R.id.num)
    TextView num;
    @BindView(R.id.iv_right)
    ImageView ivRight;

    List<BaseMvpFragment> fragmentMainItems;

    IndexFragmentStateAdapter indexFragmentStateAdapter;
    String perName;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_main_shop_tab;
    }

    @Override
    protected void initView() {
        pageTitle.setText("商城");
    }

    @Override
    public void initData() {
        perName = getIntent().getStringExtra("typeName");

        mPresenter = new ShopGoodsContract.Presenter();
        mPresenter.attachView(this);
        mPresenter.getBaseModels();
    }

    private void setType(List<Type> mTypes) {
        ivRight.setImageResource(R.mipmap.search);
        ivRight.setVisibility(View.VISIBLE);
        fragmentMainItems = new ArrayList<>();
        for (Type topType : mTypes) {
            fragmentMainItems.add(FragmentShopItem.getInstance(topType));
        }
        tabLayout.setupWithViewPager(viewPager);
        viewPager.setOffscreenPageLimit(1);
        indexFragmentStateAdapter = new IndexFragmentStateAdapter(getSupportFragmentManager(), fragmentMainItems);
        viewPager.setAdapter(indexFragmentStateAdapter);
        viewPager.setCurrentItem(0);
        //设置成功了,把刷新禁止了
        refreshlayout.setEnableRefresh(false);


        //设置选中状态
        if (TextUtils.isEmpty(perName)) {
            perName = SPUtils.getLastSelectMoudle(mContext);
        }
        if (perName != null && (
                perName.equals("教师招聘")
                        || perName.equals("公务员")
                        || perName.equals("事业单位"))) {
            for (Type baseType : mTypes) {
                if (perName.equals(baseType.getName())) {
                    viewPager.setCurrentItem(mTypes.indexOf(baseType));
                }
            }
        }
    }

    @OnClick({R.id.back, R.id.shopping_car, R.id.iv_right})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.shopping_car:
                startActivity(new Intent(this, ShoppingCarActivity.class));
                break;
            case R.id.iv_right:
                if (indexFragmentStateAdapter != null
                        && indexFragmentStateAdapter.getCurrentFragment() != null) {
                    FragmentShopItem fragmentShopItem = (FragmentShopItem) indexFragmentStateAdapter.getCurrentFragment();
                    SearchActivity.start(mContext,
                            5,
                            fragmentShopItem.baseType.getId(),
                            fragmentShopItem.baseType.getName());
                }

                break;
        }

    }

    @Override
    public void setBaseType(List<Type> baseTypes) {
        setType(baseTypes);
    }

    @Override
    public void setBooksList(ListWithPage<ShopItem> booksList) {

    }

    @Override
    public void setBanners(ArrayList<MyBanner> banners) {

    }

}
